package com.bamboo.tool.config;

import com.bamboo.tool.config.model.ApplicationConfig;
import com.bamboo.tool.template.torna.entity.TornaSetting;
import com.bamboo.tool.template.yapi.entity.YapiSetting;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

@State(name = "restfulCloudTemplateConfig", storages = {@Storage("restfulCloudTemplateConfig.xml")})
public class RestfulCloudApplicationCache implements PersistentStateComponent<ApplicationConfig> {

    private ApplicationConfig applicationConfig;

    @Override
    public @Nullable ApplicationConfig getState() {
        if (Objects.isNull(applicationConfig)) {
            applicationConfig = new ApplicationConfig();
            applicationConfig.setYapiSetting(new YapiSetting());
            applicationConfig.setTornaSetting(new TornaSetting());
        }

        return applicationConfig;
    }

    @Override
    public void loadState(@NotNull ApplicationConfig state) {
        XmlSerializerUtil.copyBean(state, Objects.requireNonNull(getState()));
    }

    public static RestfulCloudApplicationCache getInstance() {
        return ApplicationManager.getApplication().getService(RestfulCloudApplicationCache.class);
    }

    public static String getDataPath() {
        return ApplicationManager.getApplication().getService(RestfulCloudApplicationCache.class).getState().getDataPath();
    }
    public static String getExportDefaultPath() {
        return ApplicationManager.getApplication().getService(RestfulCloudApplicationCache.class).getState().getExportDefaultPath();
    }
}
